package com.bingxu.flink.advancedcode;

import com.bingxu.flink.bean.WaterSensor;
import com.bingxu.flink.utils.TransFormat;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.ProcessingTimeSessionWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * @author :BingXu
 * @description :TODO
 * @date :2021/8/16 19:10
 * @modifier :
 */

public class WindowReduce {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.setInteger("port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);

        env
                .socketTextStream("localhost", 9999)
                .map((MapFunction<String, WaterSensor>) value -> {
                    String[] fields = value.split(",");
                    return new WaterSensor(fields[0], Long.valueOf(fields[1]), Integer.valueOf(fields[2]));
                })
                .keyBy(ele -> ele.getId())
                .window(ProcessingTimeSessionWindows.withGap(Time.seconds(5)))
                .reduce(
                        new ReduceFunction<WaterSensor>() {
                            @Override
                            public WaterSensor reduce(WaterSensor value1, WaterSensor value2) throws Exception {
                                value1.setVc(value1.getVc() + value2.getVc());
                                return value1;
                            }
                        }, new WindowFunction<WaterSensor, String, String, TimeWindow>() {
                            /**
                             * TODO：当窗口关闭的时候执行该方法
                             * @param s key的值
                             * @param window 窗口
                             * @param input 输入
                             * @param out 输出
                             * @throws Exception
                             */
                            @Override
                            public void apply(String s, TimeWindow window,
                                              Iterable<WaterSensor> input,
                                              Collector<String> out) throws Exception {
                                WaterSensor waterSensor = input.iterator().next();
                                out.collect(window+""+waterSensor);

                            }
                        }
                ).print();

        env.execute();
    }
}
